package com.zl.org.service.impl;


import com.zl.basic.exception.BusinessException;
import com.zl.basic.service.impl.BaseServiceImpl;
import com.zl.basic.util.BaiduAuditUtils;
import com.zl.basic.util.StrUtils;
import com.zl.org.domain.Employee;
import com.zl.org.domain.Shop;
import com.zl.org.domain.ShopAuditLog;
import com.zl.org.dto.ShopDto;
import com.zl.org.mapper.EmpMapper;
import com.zl.org.mapper.ShopAuditLogMapper;
import com.zl.org.mapper.ShopMapper;
import com.zl.org.service.ShopService;
import com.zl.user.domain.LoginInfo;
import com.zl.user.mapper.LoginInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    public void settlement(Shop shop) {
        if(StringUtils.isEmpty(shop.getName())||StringUtils.isEmpty(shop.getEmployee().getUsername())||
                StringUtils.isEmpty(shop.getEmployee().getEmail())||StringUtils.isEmpty(shop.getEmployee().getPhone())||
                StringUtils.isEmpty(shop.getEmployee().getPassword())){
            throw new BusinessException("数据不能为空！！！");
        }
        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if (!boo){
            throw new BusinessException("店铺名称不合规！！！");
        }


        Shop shopTmp = shopMapper.loadByMessage(shop);
        if (shopTmp!=null){
            throw new BusinessException("已经入驻，请直接登录，忘记密码请联系管理员");
        }

        //1.添加t_employee
        Employee employee = shop.getEmployee();
        //2.生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //添加盐值
        employee.setSalt(salt);

        //保存logininfo信息
        LoginInfo logininfo = employee2Logininfo(employee);
        loginInfoMapper.save(logininfo);
        //保存员工信息
        employee.setLogininfo_id(logininfo.getId());
        empMapper.save(employee);

        //2.添加t_shop
        shop.setAdmin_id(employee.getId());
        shopMapper.save(shop);

        //3.修改t_employee的shop_id
        employee.setShop_id(shop.getId());
        empMapper.update(employee);
    }


    private LoginInfo employee2Logininfo(Employee employee) {
        LoginInfo logininfo = new LoginInfo();
        BeanUtils.copyProperties(employee,logininfo);
        //设置为管理员
        logininfo.setType(0);
        return logininfo;
    }


    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送邮件：审核通过邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("747485784@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<img src='http://123.207.27.208"+shop.getLogo()+"' >" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //设置收件人
        Employee employee = empMapper.loadById(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        helper.setTo("2736920793@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }



    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());

        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //3.发送邮件：审核驳回邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("747485784@qq.com");
        //设置主题
        helper.setSubject("店铺驳回邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +

                "<a href='http://localhost:8081/#/register'>点击重新入驻</a>",true);
        //设置收件人
        Employee employee = empMapper.loadById(shop.getAdmin_id());
        //helper.setTo(employee.getEmail());
        helper.setTo("2096837209@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
        empMapper.remove(shop.getAdmin_id());
        shopMapper.remove(shop.getId());
    }

    @Override
    public List<ShopDto> getShopState() {
        return shopMapper.getShopState();
    }

}
